﻿
#include "Scene0912.h"
#include "SimpleAudioEngine.h"
#include "Util.h"


Scene* Scene0912::createScene()
{
	return Scene0912::create();
}

bool Scene0912::init()
{
	if (!Scene::init())
	{
		return false;
	}

	//简单精灵创建
	Sprite *sprite1 = Sprite::create("res/plane.png");
	sprite1->setPosition(VISIBLE_SIZE / 2);
	addChild(sprite1);
	
	//锚点



	//精灵帧缓存
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("res/cityscene.plist","res/cityscene.png");

	auto background = Sprite::createWithSpriteFrameName("background.png");
	background->setPosition(VISIBLE_SIZE / 2);
	background->setScaleX(VISIBLE_SIZE.width / background->getBoundingBox().size.width);
	addChild(background);
	
	//创建人物精灵(通过精灵帧的名称创建)


	//让小人走起来
	//首先调用自定义的方法，获取动画所需的所有精灵帧
	Vector<SpriteFrame*> vectors = getSpriteFrames("capguy/walk/%04d.png",1, 8);

	//获取精灵帧组里面的第一帧来创建精灵
	Sprite *person = Sprite::createWithSpriteFrame(vectors.front());//.fornt获取第一个元素
	person->setPosition(Vect(100, 270));
	addChild(person);

	Animation* animation = Animation::createWithSpriteFrames(vectors, 1.0f / 8);
	person->runAction(RepeatForever::create(Animate::create(animation)));

	// Sequence 动画序列
	//MoveTo* moveTo = MoveTo::create(5,Vect (500, 270));
	//JumpTo* jumpTo = JumpTo::create(5, Vect(1000, 270),200,3);
	//Sequence* action = Sequence::create(moveTo, jumpTo, NULL);
	//person->runAction(action);

	// Spawn 动画融合
	//Spawn* action = Spawn::create(moveTo, jumpTo, NULL);
	//person->runAction(action);

	MoveTo* moveTo = MoveTo::create(4, Vect(1000, 270));
	CallFunc* callFunc = CallFunc::create(CC_CALLBACK_0(Scene0912::animationFinish,this));
	Sequence* action = Sequence::create(moveTo, callFunc, NULL);
	person->runAction(action);
	return true;
}

void Scene0912::animationFinish() {
	log("动画结束....");
}

Vector<SpriteFrame*> Scene0912::getSpriteFrames(const char* format,int start,int count) {
	Vector<SpriteFrame*> vectors;
	for (int i = start; i < start + count; i++)
	{
		SpriteFrame* sf = SpriteFrameCache::getInstance()->getSpriteFrameByName(StringUtils::format(format, i));
		vectors.pushBack(sf);
	}
	return vectors;
}